package com.nuanshui.heatedloan.controller.display.credit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.credit.constant.CalculateLoanAmtResponse;
import com.nuanshui.heatedloan.credit.facade.ApplyOrderService;
import com.nuanshui.heatedloan.credit.form.PayOrderInfoForm;
import com.nuanshui.heatedloan.credit.response.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by wangxw on 2017/7/12.
 * 生成订单相关的接口
 */


/***
 * 获取分期贷数据相关接口
 */
@RequestMapping("credit/order")
@RestController
public class CustomerOrderController extends BaseController implements SmartInitializingSingleton {

    private Logger logger = LoggerFactory.getLogger(CustomerOrderController.class);

    @Reference(url = GlobalParam.CREDIT_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private ApplyOrderService applyOrderService;

    /***
     * 获取单期贷款首页
     * @return
     */
    @RequestMapping(value = "/getSinglePage",method = RequestMethod.POST)
    public String getSingleLoanPageData() {
        String userId = getCurrentUser().getId();
        SingleLoanPageResponse singleLoanPageResponse = applyOrderService.getSingleLoanPageData(userId);
        return success(singleLoanPageResponse);
    }

    /***
     * 获取闪电贷的首页
     * @return
     */
    @RequestMapping(value = "/getLightPage",method = RequestMethod.POST)
    public String getLightLoanPageData() {
        LightLoanPageResponse lightLoanPageResponse=new LightLoanPageResponse();
        String userId = getCurrentUser().getId();
        lightLoanPageResponse = applyOrderService.getLightLoanPageData(userId);
        return success(lightLoanPageResponse);
    }

    @RequestMapping(value = "/getZeroDays",method = RequestMethod.POST)
    public String getZeroDays(int type) {
        String userId = getCurrentUser().getId();
        OrderResponse orderResponse = applyOrderService.getZeroDays(type, userId);
        return success(orderResponse);
    }

    @RequestMapping(value = "/place",method = RequestMethod.POST)
    public String placeOrder(String orderData, String smsCode, String phone) {
        String userId = getCurrentUser().getId();

        logger.info("CustomerOrderController placeOrder:"+JSON.toJSONString(getUserAgent()));
        PayOrderInfoForm payOrderInfoForm = JSON.parseObject(orderData, PayOrderInfoForm.class);
        payOrderInfoForm.setUserId(userId);
        if (payOrderInfoForm.getMixCardNo() == null) {
            return error("请传入银行卡号");
        }
        OrderResponse orderResponse = applyOrderService.placeOrder(payOrderInfoForm, smsCode, phone);
        return success(orderResponse);
    }

    @RequestMapping(value = "/calculateLoanAmt",method = RequestMethod.POST)
    public String calculateLoanAmt(String dataStr) {
        String userId = getCurrentUser().getId();
        if (SysStringUtils.isEmpty(dataStr)) {
            return error("请输入参数");
        }
        PayOrderInfoForm payOrderInfoForm = JSON.parseObject(dataStr, PayOrderInfoForm.class);
        payOrderInfoForm.setUserId(userId);
        CalculateLoanAmtResponse calculateLoanAmtResponse = applyOrderService.calculateLoanAmt(payOrderInfoForm);
        return success(calculateLoanAmtResponse);
    }

    /***
     * 确认订单回调接口
     * @param data
     * @return
     */
    @RequestMapping(value = "/callBackRisk",method = RequestMethod.POST)
    public String callBackRisk(@RequestBody String data) {
        logger.info("====================接受参数为" + data + "=============================");
        ApplyLoanResponse applyLoanResponse = JSON.parseObject(data, ApplyLoanResponse.class);
        applyOrderService.sendMqFromRiskConfirmOrder(applyLoanResponse);
        //logger.info("====================接受参数为" + (String)applyLoanResponse.get("orderId") + "=============================");
        return success();
    }

    @RequestMapping(value = "/raiseLimit",method = RequestMethod.POST)
    public String callBackRaiseLimit(@RequestBody String data){
        logger.info("====================接受参数为" + data + "=============================");
        RaiseLimitResponse raiseLimitResponse=JSON.parseObject(data,RaiseLimitResponse.class);
        applyOrderService.sendMqFromRaiseLimit(raiseLimitResponse);
        return success();
    }


    @Override
    public void afterSingletonsInstantiated() {
//        ApplyLoanResponse applyLoanResponse = JSON.parseObject(data, ApplyLoanResponse.class);
//        applyOrderService.sendMqFromRiskConfirmOrder(applyLoanResponse);
    }
}
